package io.renren.modules.efinance.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.efinance.dao.AccountflowDao;
import io.renren.modules.efinance.entity.AccountEntity;
import io.renren.modules.efinance.entity.AccountflowEntity;
import io.renren.modules.efinance.entity.BankcardEntity;
import io.renren.modules.efinance.service.AccountService;
import io.renren.modules.efinance.service.AccountflowService;
import io.renren.modules.efinance.service.BankcardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("accountflowService")
@Slf4j
public class AccountflowServiceImpl extends ServiceImpl<AccountflowDao, AccountflowEntity> implements AccountflowService {

    @Autowired
    private BankcardService bankcardService;
    @Autowired
    private AccountService accountService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<AccountflowEntity> queryWrapper = new QueryWrapper<AccountflowEntity>();
        Object starttime1 = params.get("starttime");
        if (starttime1 != null && !StringUtils.isEmpty((String) starttime1)) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date starttime = format.parse((String) starttime1);
                queryWrapper.ge("accountflow_time", starttime);
            } catch (ParseException e) {
                log.error("日期转换失败");
            }
        }
        Object endtime1 = params.get("endtime");
        if (endtime1 != null && !StringUtils.isEmpty((String) endtime1)) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date endtime = format.parse((String) endtime1);
                queryWrapper.ge("accountflow_time", endtime);
            } catch (ParseException e) {
                log.error("日期转换失败");
            }
        }
//        if (endtime1 != null && !StringUtils.isEmpty((String) endtime1)) {
//            Date starttime = (Date) params.get("starttime");
//            queryWrapper.ge("accountflow_time", starttime);
//        }
//        Date endtime = (Date) params.get("endtime");
//        if (starttime != null) {
//            queryWrapper.le("accountflow_time", endtime);
//        }
//        String type = (String) params.get("type");
//        if (!StringUtils.isEmpty(type)){
//            if (!type.equals("全部")) {
//                queryWrapper.eq("accountflow_type", type);
//            }
//        }
        queryWrapper.and(w -> w.eq("accountflow_payeeaccount", ShiroUtils.getUserEntity().getUsername())
                .or().eq("accountflow_payeraccount", ShiroUtils.getUserEntity().getUsername()));
        IPage<AccountflowEntity> page = this.page(
                new Query<AccountflowEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

//    @Transactional
//    @Override
//    public void recharge(AccountflowEntity accountflow) {
//        // 银行卡余额扣减
//        BankcardEntity bankcard = new BankcardEntity();
//        bankcard.setBankcardId(accountflow.getBankcardIdcard());
//        bankcard.setBankcardBalance(accountflow.getAccountflowMoney());
//        int i = bankcardService.recharge(bankcard);
//        if(i<=0)
//            throw new RuntimeException("余额不足");
//        // 电子账户余额增加
//        AccountEntity accountEntity = new AccountEntity();
//        accountEntity.setAccountBalance(accountflow.getAccountflowMoney());
//        accountEntity.setAccountName(accountflow.getAccountflowPayeraccount());
//        i=accountService.recharge(accountEntity);
//        if(i<=0){
//            log.error("账户余额增加失败i={}",i);
//            throw new RuntimeException("未知异常");
//        }
//        // 插入流水记录
//        baseMapper.insert(accountflow);
//    }

}